<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['wx:require:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:require:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:require:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:require:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="requireList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="wxUserId" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phone"/>
      <el-table-column label="年龄" align="center" prop="age" >
        <template slot-scope="scope">
          <span>{{scope.row.minAge}}~{{scope.row.maxAge}}</span>
        </template>
      </el-table-column>
      <el-table-column label="身高" align="center" prop="height" >
        <template slot-scope="scope">
          <span>{{scope.row.minHeight}}~{{scope.row.maxHeight}}</span>
        </template>
      </el-table-column>
      <el-table-column label="体重" align="center" prop="weight" >
        <template slot-scope="scope">
          <span>{{scope.row.minWeight}}~{{scope.row.maxWeight}}</span>
        </template>
      </el-table-column>
      <el-table-column label="收入" align="center" prop="income" />
      <el-table-column label="居住地" align="center" prop="habit" />
      <el-table-column label="户籍地" align="center" prop="birthPlace" />
      <el-table-column label="婚姻状况" align="center" prop="marriage">
        <template slot-scope="scope">
          <span v-if="scope.row.marriage==0">未婚</span>
          <span v-if="scope.row.marriage==1">离异</span>
        </template>
      </el-table-column>
      <el-table-column label="有无子女" align="center" prop="child">
        <template slot-scope="scope">
          <span v-if="scope.row.marriage==0">无子女</span>
          <span v-if="scope.row.marriage==1">有子女</span>
        </template>
      </el-table-column>
      <el-table-column label="星座" align="center" prop="constellation" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['wx:require:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['wx:require:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改择偶标准表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-form-item label="择偶年龄" prop="age">
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.minAge" placeholder="请输入择偶年龄" />
            </el-col>
            <el-col :span="4" style="text-align: center">~</el-col>
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.maxAge" placeholder="请输入择偶年龄" />
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="择偶身高" prop="height" >
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.minHeight" placeholder="请输入择偶身高" />
            </el-col>
            <el-col :span="4" style="text-align: center">~</el-col>
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.maxHeight" placeholder="请输入择偶身高" />
            </el-col>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="择偶体重" prop="weight">
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.minWeight" placeholder="请输入择偶体重" />
            </el-col>
            <el-col :span="4" style="text-align: center">~</el-col>
            <el-col :span="10" style="text-align: left">
              <el-input v-model="form.maxWeight" placeholder="请输入择偶体重" />
            </el-col>
          </el-form-item>
        </el-row>
        <el-form-item label="收入" prop="income">
          <el-input v-model="form.income" placeholder="请输入收入" />
        </el-form-item>
        <el-form-item label="居住地" prop="habit">
          <el-input v-model="form.habit" placeholder="请输入居住地" />
        </el-form-item>
        <el-form-item label="户籍地" prop="birthPlace">
          <el-input v-model="form.birthPlace" placeholder="请输入户籍地" />
        </el-form-item>
        <el-form-item label="婚姻状况" prop="marriage">
          <el-select v-model="form.marriage" placeholder="请选择" >
            <el-option-group >
              <el-option label="未婚" value="0" />
              <el-option label="离异" value="1" />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="有无子女" prop="child">
          <el-select v-model="form.child" placeholder="请选择" >
            <el-option-group >
              <el-option label="有子女" value="1" />
              <el-option label="无子女" value="0" />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="星座" prop="constellation">
          <el-select v-model="form.constellation" placeholder="请选择星座" >
            <el-option-group >
              <el-option label="摩羯座" value="摩羯座" />
              <el-option label="水瓶座" value="水瓶座" />
              <el-option label="双鱼座" value="双鱼座" />
              <el-option label="白羊座" value="白羊座" />
              <el-option label="金牛座" value="金牛座" />
              <el-option label="双子座" value="双子座" />
              <el-option label="巨蟹座" value="巨蟹座" />
              <el-option label="狮子座" value="狮子座" />
              <el-option label="处女座" value="处女座" />
              <el-option label="天秤座" value="天秤座" />
              <el-option label="天蝎座" value="天蝎座" />
              <el-option label="射手座" value="射手座" />
            </el-option-group>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRequire, getRequire, delRequire, addRequire, updateRequire, exportRequire } from "@/api/wx/require";

export default {
  name: "Require",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 择偶标准表表格数据
      requireList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maxAge: undefined,
        minAge: undefined,
        minHeight: undefined,
        maxHeight: undefined,
        income: undefined,
        habit: undefined,
        birthPlace: undefined,
        marriage: undefined,
        child: undefined,
        minWeight: undefined,
        maxWeight: undefined,
        constellation: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询择偶标准表列表 */
    getList() {
      this.loading = true;
      listRequire(this.queryParams).then(response => {
        this.requireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wxUserId: undefined,
        maxAge: undefined,
        minAge: undefined,
        minHeight: undefined,
        maxHeight: undefined,
        income: undefined,
        habit: undefined,
        birthPlace: undefined,
        marriage: undefined,
        child: undefined,
        minWeight: undefined,
        maxWeight: undefined,
        constellation: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wxUserId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加择偶标准表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wxUserId = row.wxUserId || this.ids
      getRequire(wxUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改择偶标准表";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wxUserId != undefined) {
            updateRequire(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addRequire(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wxUserIds = row.wxUserId || this.ids;
      this.$confirm('是否确认删除择偶标准表编号为"' + wxUserIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRequire(wxUserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有择偶标准表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRequire(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
